using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;
using MagnoliaTableAdapters;

public partial class ComboList : System.Web.UI.Page
{
    static ArrayList globalResults = new ArrayList();
    static int globalResults_SI = -1;
    static int globalDefault_SI = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = ConfigurationManager.AppSettings["PageTitle"] + " - Combo List";
        ListBox_Results.Attributes.Add("onchange", "selectedIndexChange();");
        Label_ComboAlerts.Style.Add("display", "none");

        // hide new button for user access
        Btn_New.Style.Add("display", "none");

        // get user access permissions
        DataTable UserAccessDT = (DataTable)Session["UserAccess"];
        foreach (DataRow dr in UserAccessDT.Rows)
        {
            // new user
            if (Convert.ToString(dr.ItemArray[0]) == "404")
            {
                Btn_New.Style.Add("display", "block");
            }
        }

        if (!Page.IsPostBack)
        {
            Magnolia.CHANNELDataTable Channels = new Magnolia.CHANNELDataTable();
            CHANNELTableAdapter ChannelAdapter = new CHANNELTableAdapter();
            ChannelAdapter.Fill(Channels);

            DropDown_DefaultCombo.DataSource = Channels;
            DropDown_DefaultCombo.DataTextField = Channels.NameColumn.ColumnName;
            DropDown_DefaultCombo.DataValueField = Channels.ComboIDColumn.ColumnName;
            DropDown_DefaultCombo.DataBind();
        }

        Btn_Edit.Attributes.Add("disabled", "disabled");

        PreserveListItems();
    }
    protected void Btn_Search_Click(object sender, EventArgs e)
    {
        DDList_Sort.SelectedIndex = 0;
        ListBox_Results.Items.Clear();
        globalResults.Clear();

        // query to get a list of items
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
            conn.Open();

            String sProc = "sproc_WEB_GetListCombo";

            SqlCommand cmd = new SqlCommand(sProc, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            // set paramter
            DbParameter param = cmd.CreateParameter();
            param.ParameterName = "@XML";
            param.Value = Session["ListSearchString"];
            param.DbType = DbType.Xml;
            cmd.Parameters.Add(param);

            param = cmd.CreateParameter();
            param.ParameterName = "@IncludeAssets";
            param.Value = false;
            param.DbType = DbType.Boolean;
            cmd.Parameters.Add(param);

            param = cmd.CreateParameter();
            param.ParameterName = "@IncludeDisabled";
            param.Value = Convert.ToBoolean(Session["ComboListSearchIncludeDisabled"]);
            param.DbType = DbType.Boolean;
            cmd.Parameters.Add(param);

            // execute the stored procedure and return the results
            XmlReader xReader = cmd.ExecuteXmlReader();
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(xReader);
            xReader.Close();

            XmlNodeList nodes = xDoc.SelectNodes("//ASSET");
            //globalResults.Clear();
            if (nodes.Count == 0)
            {
                Label_ComboAlerts.Text = "No results were found for the given search criteria.";
                Label_ComboAlerts.Style["color"] = "Green";
                Label_ComboAlerts.Style["font-weight"] = "bold";
                Label_ComboAlerts.Style["display"] = "block";
                return;
            }
            foreach (XmlNode xNode in nodes)
            {
                ListItem item = new ListItem();
                item.Text = Server.HtmlDecode(Convert.ToString(xNode.Attributes["Name"].InnerXml));
                item.Value = Convert.ToString(xNode.Attributes["ID"].InnerXml);
                String ThisEnabled = Convert.ToString(xNode.Attributes["ThisEnabled"].InnerXml);
                if (ThisEnabled == "0")
                    item.Attributes.Add("class", "disabledOption");
                try
                {
                    if ((xNode.HasChildNodes) && (xNode.FirstChild.Attributes.Count > 0))
                    {
                        item.Attributes.Add("label", Convert.ToString(xNode.FirstChild.Attributes["CreatedOn"].InnerXml));
                    }
                    else
                    {
                        item.Attributes.Add("label", "");
                    }
                }
                catch (Exception exception)
                {
                    String err = exception.Message;
                }

                ListBox_Results.Items.Add(item);
                globalResults.Add(item);
            }
            ListBox_Results.SelectedIndex = -1;
        }
        catch (Exception exception)
        {
            String err = exception.Message;
        }
    }

    protected void Btn_New_Click(object sender, EventArgs e)
    {
        Hidden_BtnChoice.Value = "New";
    }
    protected void Btn_Edit_Click(object sender, EventArgs e)
    {
        Hidden_BtnChoice.Value = "Edit";
    }
    protected void Btn_EditDefaultCombo_Click(object sender, EventArgs e)
    {
        Hidden_BtnChoice.Value = "EditDefault";
    }
    protected void DDList_Sort_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDList_Sort.SelectedValue == "1")
        {
            globalResults.Sort(new SortListByTextAsc());
            ListBox_Results.Items.Clear();
            for (int i = 0; i < globalResults.Count; i++)
            {
                ListBox_Results.Items.Add((ListItem)globalResults[i]);
            }
        }
        else if (DDList_Sort.SelectedValue == "2")
        {
            globalResults.Sort(new SortListByIDDesc());
            ListBox_Results.Items.Clear();
            for (int i = 0; i < globalResults.Count; i++)
            {
                ListBox_Results.Items.Add((ListItem)globalResults[i]);
            }
        }
        ListBox_Results.SelectedIndex = -1;
    }
    protected void PreserveListItems()
    {
        globalResults_SI = ListBox_Results.SelectedIndex;
        ListBox_Results.Items.Clear();
        for (int i = 0; i < globalResults.Count; i++)
        {
            ListBox_Results.Items.Add((ListItem)globalResults[i]);
        }
        ListBox_Results.SelectedIndex = globalResults_SI;
        
    }
    protected void DropDown_DefaultCombo_SelectedIndexChanged(object sender, EventArgs e)
    {
        globalDefault_SI = DropDown_DefaultCombo.SelectedIndex;
    }
}
